package com.zoweunion.mechanic.controller;

import java.util.Date;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import com.zoweunion.mechanic.controller.BaseController;
import com.zoweunion.mechanic.util.constants.Constants;
import com.zoweunion.mechanic.util.MyException;
import com.zoweunion.mechanic.service.RoleUserService;
import com.zoweunion.mechanic.entity.RoleUserEntity;


import com.zoweunion.mechanic.model.ResponseBean;
import com.github.pagehelper.PageInfo;

import javax.servlet.http.HttpServletRequest;


/**
 * 生产商用户角色表
 *
 * @author xp
 * @email 17688825585@163.com
 * @date 2020-09-28 11:00:10
 */
@RestController
@RequestMapping("/roleuser")
public class RoleUserController extends BaseController {
    @Autowired
    private RoleUserService roleUserService;

    /**
     * 列表
     */
    @GetMapping("/roleUserList")
    public ResponseBean roleUserList(@RequestParam Map<String, Object> params, HttpServletRequest request) throws MyException {
        params.put("s_id", getCurrentUser(request).getS_id());
        params.put("user_id", getCurrentUser(request).getId());
        PageInfo info = roleUserService.queryPage(params);
        return new ResponseBean(Constants.CODE_SUCCESS, "操作成功", info);
    }

    /**
     * 新增
     */
    @PostMapping("/roleUserInsertUpdate")
    public ResponseBean roleUserInsertUpdate(@RequestBody RoleUserEntity params,
                                             HttpServletRequest request) throws MyException {
        params.setSId(getCurrentUser(request).getS_id());
        if (params.getId() != null) {
            roleUserService.updateById(params);
        } else {
            params.setCreateTime(new Date());
            params.setDeleteFlag(0);
            roleUserService.save(params);
        }
        return new ResponseBean(Constants.CODE_SUCCESS, "操作成功", "");
    }

    /**
     * 列表树
     */
    @GetMapping("/roleUserListTree")
    public ResponseBean roleUserListTree(@RequestParam Map<String, Object> params, HttpServletRequest request) throws MyException {
        params.put("s_id", getCurrentUser(request).getS_id());
        params.put("pid", 0);
        List<RoleUserEntity> info = roleUserService.roleUserListTree(params);
        return new ResponseBean(Constants.CODE_SUCCESS, "操作成功", info);
    }
}
